<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="../dist/css/bootstrap.min.css">
  <title>表单验证-客户端校验</title>
</head>
<body class="p-3">
  <form class="row g-3" novalidate>
    <div class="col-6">
      <label for="phone" class="form-label">手机</label>
      <input type="tel" class="form-control" id="phone" required pattern="^1[0-9]{10}$">
      <div class="invalid-feedback">请输入手机号码</div>
    </div>
    <div class="col-6">
      <label for="username" class="form-label">用户名</label>
      <div class="input-group has-validation">
        <span class="input-group-text">@</span>
        <input type="text" class="form-control" id="username" required>
        <div class="invalid-feedback">请输入用户名</div>
      </div>
    </div>
    <div class="col-4">
      <label for="province" class="form-label">省份</label>
      <select class="form-select" id="province" required>
        <option selected disabled value="">请选择</option>
        <option>...</option>
      </select>
      <div class="invalid-feedback">请选择省份</div>
    </div>
    <div class="col-4">
      <label for="city" class="form-label">城市</label>
      <input type="text" class="form-control" id="city" required>
      <div class="invalid-feedback">请输入城市</div>
    </div>
    <div class="col-4">
      <label for="address" class="form-label">详细地址</label>
      <input type="text" class="form-control" id="address" required>
      <div class="invalid-feedback">请输入详细地址</div>
    </div>
    <div class="col-12">
      <div class="form-check">
        <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
        <label class="form-check-label" for="invalidCheck">
          同意使用协议
        </label>
        <div class="invalid-feedback">请同意后提交</div>
      </div>
    </div>
    <div class="col-12">
      <button class="btn btn-primary" type="submit">提交</button>
    </div>
  </form>
  <script src="../dist/jquery-3.6.0.min.js"></script>
  <script>
    $(function(){
      $('form').bind('submit', function(){
        let $this = $(this);
        if (!$this[0].checkValidity()) {
          $this.addClass('was-validated');
          return false;
        }
      })
    })
  </script>
</body>
</html>